package com.yenlien.traceyun.control.dao;

import com.yenlien.traceyun.control.entity.ModelCategory;
import com.yenlien.traceyun.webase.dao.BaseJpaDao;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * @author: bigwangy@sina.com
 * @create: 2020-11-18
 * @description:
 **/
public interface ModelCategoryDao extends BaseJpaDao<ModelCategory> {

    public List<ModelCategory> findByIdIn(Collection<String> ids);

    /**
     * 查询根节点
     *
     * @return
     */
    @Query("select mc from ModelCategory mc where mc.parentId is null or mc.parentId='' order by mc.sortNum asc")
    public List<ModelCategory> findRoot();

    /**
     * 根究parentId删除
     *
     * @param parentId
     */
    public void deleteByParentId(String parentId);

    @Query("select mc from ModelCategory mc where mc.id=?1")
    public ModelCategory findParent(String childId);


    /**
     * 根据名称查找
     *
     * @param name
     * @return
     */
    public ModelCategory findByName(String name);

    /**
     * 有序排列，用于递归
     *
     * @return
     */
    @Query("select mc from ModelCategory mc order by mc.type asc,mc.sortNum asc")
    public List<ModelCategory> findSortAll();

    /**
     * 查找child
     *
     * @param parentId
     * @return
     */
    public List<ModelCategory> findByParentId(String parentId);


    /**
     * 根据ids
     *
     * @param ids
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteByIdIn(List<String> ids);

    @Query(value = "select * from t_model_category c where (c.parent_id is null or c.parent_id='') and  c.id in ( select  cm.category_id from t_company_model_category cm where cm.company_id =?1) ORDER BY sort_num ", nativeQuery = true)
    public List<ModelCategory> findRootByCompanyId(String companyId);

    @Query(value = "select * from t_model_category c where (c.parent_id is null or c.parent_id='') and  c.id in ( select  cm.category_id from t_company_customapp_version_category cm where cm.custom_app_version_id =?1) ORDER BY sort_num ", nativeQuery = true)
    public List<ModelCategory> findRootByCustomAppVersionId(String customAppVersionId);

    @Query(value = "select * from t_model_category c where  c.id in ( select  cm.category_id from t_company_model_category cm where cm.company_id =?1) ORDER BY sort_num ", nativeQuery = true)
    public List<ModelCategory> findByCompanyId(String companyId);

    /**
     * 政管
     *
     * @param companyId
     * @return
     */
    // 原sql：
//   @Query(value = "SELECT * FROM t_model_category c WHERE ( c.parent_id IS NULL OR c.parent_id = '' ) AND c.id IN ( SELECT cm.category_id FROM t_company_customapp_version_category cm WHERE cm.custom_app_version_id IN ( SELECT DISTINCT custom_app_version_id FROM t_company_customapp_version WHERE company_id = ?1 AND app_id = '001' ) )  ORDER BY sort_num", nativeQuery = true)
   // 2022-5-5 修改
    @Query(value = "SELECT * FROM t_model_category c WHERE ( c.parent_id IS NULL OR c.parent_id = '' ) AND c.id IN ( SELECT cm.category_id FROM t_company_customapp_version_category cm WHERE cm.custom_app_version_id IN ( SELECT DISTINCT custom_app_version_id FROM t_company_customapp_version WHERE company_id = ?1) )  ORDER BY sort_num", nativeQuery = true)
    List<ModelCategory> getByCompanyId(String companyId);

    /**
     * 企业
     *
     * @param companyId
     * @return
     */
    @Query(value = "SELECT * FROM t_model_category c WHERE ( c.parent_id IS NULL OR c.parent_id = '' ) AND c.id IN ( SELECT cm.category_id FROM t_company_customapp_version_category cm WHERE cm.custom_app_version_id IN ( SELECT DISTINCT custom_app_version_id FROM t_company_customapp_version WHERE company_id = ?1 )) ORDER BY sort_num", nativeQuery = true)
    List<ModelCategory> getModelCategory(String companyId);

    @Query(value = "SELECT mc.* FROM t_model_category mc WHERE mc.parent_id = ?1", nativeQuery = true)
    List<ModelCategory> get2Level(String id);


}
